package cn.iocoder.yudao.module.scrm.controller.admin.customerassistants;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.scrm.controller.admin.customerassistants.vo.*;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customerassistants.CustomerAssistantsDO;
import cn.iocoder.yudao.module.scrm.service.customerassistants.CustomerAssistantsService;

@Tag(name = "管理后台 - 客户协作人")
@RestController
@RequestMapping("/scrm/customer-assistants")
@Validated
public class CustomerAssistantsController {

    @Resource
    private CustomerAssistantsService customerAssistantsService;

    @PostMapping("/create")
    @Operation(summary = "创建客户协作人")
    @PreAuthorize("@ss.hasPermission('scrm:customer-assistants:create')")
    public CommonResult<Boolean> createCustomerAssistants(@Valid @RequestBody CustomerAssistantsSaveReqVO createReqVO) {
        customerAssistantsService.createCustomerAssistants(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户协作人")
    @PreAuthorize("@ss.hasPermission('scrm:customer-assistants:update')")
    public CommonResult<Boolean> updateCustomerAssistants(@Valid @RequestBody CustomerAssistantsSaveReqVO updateReqVO) {
        customerAssistantsService.updateCustomerAssistants(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户协作人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:customer-assistants:delete')")
    public CommonResult<Boolean> deleteCustomerAssistants(@RequestParam("id") Long id) {
        customerAssistantsService.deleteCustomerAssistants(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户协作人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:customer-assistants:query')")
    public CommonResult<CustomerAssistantsRespVO> getCustomerAssistants(@RequestParam("id") Long id) {
        CustomerAssistantsDO customerAssistants = customerAssistantsService.getCustomerAssistants(id);
        return success(BeanUtils.toBean(customerAssistants, CustomerAssistantsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户协作人分页")
    @PreAuthorize("@ss.hasPermission('scrm:customer-assistants:query')")
    public CommonResult<PageResult<CustomerAssistantsRespVO>> getCustomerAssistantsPage(@Valid CustomerAssistantsPageReqVO pageReqVO) {
        PageResult<CustomerAssistantsDO> pageResult = customerAssistantsService.getCustomerAssistantsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerAssistantsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户协作人 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:customer-assistants:export')")
    @OperateLog(type = EXPORT)
    public void exportCustomerAssistantsExcel(@Valid CustomerAssistantsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerAssistantsDO> list = customerAssistantsService.getCustomerAssistantsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户协作人.xls", "数据", CustomerAssistantsRespVO.class,
                        BeanUtils.toBean(list, CustomerAssistantsRespVO.class));
    }

}